package com.nf.health.app.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import com.nf.health.app.R;

public class CircleProgress extends View {

	private int width;
	private float radius;
	private Paint circlePaint;
	private float centerX;
	private float centerY;
	private RectF oval;
	private float max = 100;
	private float progress = 0;
	private float total = -1;
	private Paint arcPaint;
	private Paint textPaint;
	private Paint textHealthPaint;
	private Paint smallPaint;
	private Rect rect;
	private Rect rect1;
	boolean centerText;
	int backgroundLineWidth;
	private float smallX;
	private float smallY;

	public CircleProgress(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context,attrs);
	}

	public CircleProgress(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context, attrs);
	}

	public CircleProgress(Context context) {
		super(context);
	}

	private void init(Context context, AttributeSet attrs) {
		TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleProgress);
		int backgroundColor = typedArray.getColor(R.styleable.CircleProgress_background_color, Color.WHITE);
		int foregroundColor = typedArray.getColor(R.styleable.CircleProgress_foreground_color, Color.WHITE);
		backgroundLineWidth = typedArray.getDimensionPixelSize(R.styleable.CircleProgress_background_line_width, 2);
		int foregroundLineWidth = typedArray.getDimensionPixelSize(R.styleable.CircleProgress_foreground_line_width, 10);
		int textColor = typedArray.getColor(R.styleable.CircleProgress_text_color, Color.WHITE);
		centerText = typedArray.getBoolean(R.styleable.CircleProgress_center_text, true);


		circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		circlePaint.setColor(backgroundColor);
		circlePaint.setStrokeWidth(backgroundLineWidth);
		circlePaint.setStyle(Paint.Style.STROKE);

		arcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		arcPaint.setColor(foregroundColor);
		arcPaint.setStrokeWidth(foregroundLineWidth);
		arcPaint.setStyle(Paint.Style.STROKE);
		arcPaint.setStrokeCap(Paint.Cap.ROUND);
		arcPaint.setStyle(Paint.Style.STROKE);

		textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		textPaint.setColor(textColor);
		textPaint.setStyle(Paint.Style.FILL);
		
		textHealthPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		textHealthPaint.setColor(textColor);
		textHealthPaint.setStyle(Paint.Style.FILL);
		textHealthPaint.setTextSize(16);

		smallPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		smallPaint.setColor(Color.WHITE);

		rect = new Rect();
		rect1 = new Rect();
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

		int screenWidth = getResult(widthMeasureSpec);
		width = screenWidth / 2;
		centerX = width / 2;
		centerY = centerX;
		radius = centerX - backgroundLineWidth*2;
		oval = new RectF(centerX - radius, centerX - radius, centerX + radius,
				centerX + radius);
		textPaint.setTextSize(radius/1.5F);
		setMeasuredDimension(width, width);
	}

	private int getResult(int measureSpec){
		int result = 200;
		int specMode = MeasureSpec.getMode(measureSpec);
		int specSize =  MeasureSpec.getSize(measureSpec);
		switch (specMode) {
			case MeasureSpec.UNSPECIFIED:
				return result;
			case MeasureSpec.AT_MOST:
				result = Math.min(result, specSize);
				break;
			case MeasureSpec.EXACTLY:
				result = specSize;
				break;
		}
		return result;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawCircle(centerX, centerY, radius, circlePaint);

		canvas.drawArc(oval, -90, 360 / max * progress, false, arcPaint);
		if(centerText){
			String text = String.valueOf((int) progress);
			textPaint.getTextBounds(text, 0, text.length(), rect);
			canvas.drawText(text+"%", centerX - rect.right / 1.3f, centerY - rect.top/2, textPaint);//添加百分号,改变左侧起始位置 hepan
		}

		if(progress==total){
			if(!TextUtils.isEmpty(health)){
				textHealthPaint.getTextBounds(health, 0, health.length(), rect1);
				canvas.drawText(health, centerX - rect1.right / 2, centerY - rect.top/2-rect1.top*2, textHealthPaint);
			}
		}

		//确定小圆点的中心坐标
		smallX = (float) (centerX + radius * Math.sin(Math.toRadians(progress * 360 / max)));
		smallY = (float) (centerY - radius * Math.cos(Math.toRadians(progress * 360 / max)));
		//画小圆点
		canvas.drawCircle(smallX, smallY, (backgroundLineWidth) / 3, smallPaint);
		canvas.save();
	}

	
	public void setProgress( float setProgress, int total) {
		this.total = total;
		progress = 0;
		if (setProgress > max) {
			setProgress = max;
		} else if (progress < 0) {
			setProgress = 0;
		} else {
			this.progress = setProgress;
			postInvalidate();
		}
	}
	private String health;
	public void setHealthState(String health) {
		this.health = health;
	}

	public  float getCurrentProgress(){
		return total;
	}

}
